package com.easidea.tim.flex.remote.purchase
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.Page;
	import com.easidea.tim.flex.bean.purchase.PurchaseOrder;
	import com.easidea.tim.flex.bean.purchase.PurchaseOrderLocator;
	import com.easidea.tim.flex.bean.purchase.PurchaseReturnLocator;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	import com.easidea.tim.flex.util.AppAlert;
	import com.easidea.tim.flex.util.StringUtils;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	

	public class PurchaseOrderDao extends BaseDao
	{
		
		private var _purchaseOrderService:RemoteObject = new RemoteObject();
		[Bindable]private var purchaseOrderLocator:PurchaseOrderLocator = PurchaseOrderLocator.getInstance();
		private static var _instance:PurchaseOrderDao;
		private var _orderId:int;
		
		public static function getInstance():PurchaseOrderDao{
			if( !_instance ){
				_instance = new PurchaseOrderDao();
				
			}
			return _instance;
		}
		public function PurchaseOrderDao() {
				_purchaseOrderService.channelSet = this.buildChannelSet();
				_purchaseOrderService.destination="purchaseOrderService";
				_purchaseOrderService.concurrency = "last";
				_purchaseOrderService.makeObjectsBindable = true;
				_purchaseOrderService.showBusyCursor = true;
		}		
		
				/**
		 * 调用spring方法:分页查询采购合同列表
		 * @param name
		 * @param page
		 * 
		 */		
		public function getPurchaseOrderList(purchaseOrderNum:String, supplierName:String,startTime:Date,endTime:Date,page:Page):void {			
			_purchaseOrderService.addEventListener(ResultEvent.RESULT, showPurchaseOrderList);
			_purchaseOrderService.addEventListener(FaultEvent.FAULT, _Fault);
			_purchaseOrderService.getPurchaseOrderListByCondition(purchaseOrderNum,supplierName,startTime,endTime,page);
		}
		/**
		 * 分页查询采购合同列表回调函数
		 * @param e
		 * 
		 */			
		private function showPurchaseOrderList(e:ResultEvent):void {	
					 
			_purchaseOrderService.removeEventListener(ResultEvent.RESULT, showPurchaseOrderList);
			 if(e.result != null) {		
				var page:Page = e.result as Page;					 				 
				 Env.dispatchEvent(new CustomerEvent("getPurchaseOrderListSuccess",page));				  									 
			 } 
		}
			/**
		 * 调用spring方法:新增或修改采购合同需要的初始化数据
		 * @param name
		 * @param page
		 * 
		 */	
		public function filteSupByName(name:String):void {
			_purchaseOrderService.addEventListener(ResultEvent.RESULT, filteSupByNameSuccess);
			_purchaseOrderService.addEventListener(FaultEvent.FAULT, _Fault);
			_purchaseOrderService.filteSupByName(name);
			
		}
		/**
		 * 新增或修改采购合同时需要的初始化数据成功回调函数，数据为供应商列表
		 * @param 
		 * 
		 */		
		private function filteSupByNameSuccess(e:ResultEvent):void {			 
			_purchaseOrderService.removeEventListener(ResultEvent.RESULT, filteSupByNameSuccess);			
			 var result:ArrayCollection = e.result as ArrayCollection; 	 
			 if(result != null ) {	

				Env.dispatchEvent(new CustomerEvent("filteSupByNameSuccess",result));
			 } 
		}	
		
		/**
		 *调用spring方法:保存采购合同和明细列表 
		 * @param customer
		 * 
		 */		
		public function saveOrUpdatePurchaseOrder(order:PurchaseOrder,items:ArrayCollection):void {
				_purchaseOrderService.addEventListener(FaultEvent.FAULT, _Fault);
				_purchaseOrderService.addEventListener(ResultEvent.RESULT, savePurchaseOrderSuccess);
				_purchaseOrderService.saveOrUpdate(order,items);
		}
	   /**
	    *保存采购合同和明细列表成功回调函数 
	    * @param e
	    * 
	    */		
	   private function savePurchaseOrderSuccess(e:ResultEvent):void {			 
			_purchaseOrderService.removeEventListener(ResultEvent.RESULT, savePurchaseOrderSuccess);
			 if(e.result != null) {
				AppAlert.show("保存成功！","提示");
				getPurchaseOrderList("","",null,null,new Page());
				Env.dispatchEvent(new CustomerEvent("savePurchaseOrderSuccess"));
			 } 
		}
		/**
		 *调用spring方法:删除采购合同
		 * @param customer
		 * 
		 */		
		public function deletePurchaseOrder(id:String):void {
				_purchaseOrderService.addEventListener(FaultEvent.FAULT, _Fault);
				_purchaseOrderService.addEventListener(ResultEvent.RESULT, deletePurchaseOrderSuccess);
				_purchaseOrderService.deletePurchaseOrder(id);
		}
	   /**
	    * 删除采购合同成功回调函数 
	    * @param e
	    * 
	    */		
	   private function deletePurchaseOrderSuccess(e:ResultEvent):void {			 
			_purchaseOrderService.removeEventListener(ResultEvent.RESULT, deletePurchaseOrderSuccess);
			 if(e.result != null && e.result == "success") {
				AppAlert.show("删除成功！","提示");
				Env.dispatchEvent(new  CustomerEvent("reGetPurchaseOrderList"));
			 } 
		}
		/**
		 *调用spring方法：合同关闭 
		 * @param id
		 * @param closeMan
		 * 
		 */		
		public  function closePurchaseOrder(id:int, closeMan:String):void {
				_purchaseOrderService.addEventListener(FaultEvent.FAULT, _Fault);
				_purchaseOrderService.addEventListener(ResultEvent.RESULT, closePurchaseOrderSuccess);
				_purchaseOrderService.closePurchaseOrder(id,closeMan);				
		}
	  /**
	    * 合同关闭回调函数 
	    * @param e
	    * 
	    */		
	   private function closePurchaseOrderSuccess(e:ResultEvent):void {			 
			_purchaseOrderService.removeEventListener(ResultEvent.RESULT, closePurchaseOrderSuccess);
			 if(e.result != null) {
				AppAlert.show("合同已关闭！","提示");
			    Env.dispatchEvent(new  CustomerEvent("reGetPurchaseOrderList"));
			 } 
		}
		/**
		 * 
		 * 调用spring方法：获取产品信息
		 */		
		public  function filteProductByName(name:String):void {
				_purchaseOrderService.addEventListener(FaultEvent.FAULT, _Fault);
				_purchaseOrderService.addEventListener(ResultEvent.RESULT,filteProductByNameSuccess);
				_purchaseOrderService.filteProductByName(name);				
		}
	   /**
	    * 取产品信息成功回调函数 
	    * @param e
	    * 
	    */		
	   private function filteProductByNameSuccess(e:ResultEvent):void {			 
			_purchaseOrderService.removeEventListener(ResultEvent.RESULT, filteProductByNameSuccess);
			 if(e.result != null) {
		   		 Env.dispatchEvent(new  CustomerEvent("filteProductByNameSuccess",e.result));	    
			 } 
		}
		/**
		 * 调用spring方法：采购统计
		 * @param startDate
		 * @param endDate
		 * @param statType
		 * 
		 */			
		public  function statPurchaseOrder(startDate:Date, endDate:Date,statType:String):void {
				purchaseOrderLocator.startDate = startDate;
				purchaseOrderLocator.endDate = endDate;	
				_purchaseOrderService.addEventListener(FaultEvent.FAULT, _Fault);
		 		_purchaseOrderService.addEventListener(ResultEvent.RESULT,statPurchaseOrderSuccess);
				_purchaseOrderService.statPurchaseOrder(startDate,endDate,statType);				
		  }
		/**
		 * 采购统计回调函数
		 * @param e
		 * 
		 */		  
		private function statPurchaseOrderSuccess(e:ResultEvent):void {
			_purchaseOrderService.removeEventListener(ResultEvent.RESULT, statPurchaseOrderSuccess);
			if(e.result != null) {
				purchaseOrderLocator.statResult.removeAll();
				var metertotal:Number = 0;
				var moneyTotal:Number = 0;
				for each(var result:Array in e.result) {
					var obj:Object = new Object();
					obj.id = result[0];
					obj.name = result[1];
					obj.totalMeter = result[2];
					obj.totalAmount = result[3];
					purchaseOrderLocator.statResult.addItem(obj);
					metertotal = metertotal + Number(result[2]);
					moneyTotal = moneyTotal + Number(result[3]);
				}
				purchaseOrderLocator.meterTotal = StringUtils.formatNumString(metertotal.toFixed(1));
				purchaseOrderLocator.moneyTotal = StringUtils.formatNumString(moneyTotal.toFixed(3));
			}
		}
		/**
		 * 调用spring方法：采购统计
		 * @param startDate
		 * @param endDate
		 * @param statType
		 * 
		 */			
		public  function statPurchaseOrderByMonth(year:int, month:int):void {
				_purchaseOrderService.addEventListener(FaultEvent.FAULT, _Fault);
		 		_purchaseOrderService.addEventListener(ResultEvent.RESULT,statPurchaseOrderByMontSuccess);
				_purchaseOrderService.statPurchaseOrderByMonth(year,month);				
		  }
		/**
		 * 采购统计回调函数
		 * @param e
		 * 
		 */		  
		private function statPurchaseOrderByMontSuccess(e:ResultEvent):void {
			_purchaseOrderService.removeEventListener(ResultEvent.RESULT, statPurchaseOrderByMontSuccess);
			if(e.result != null) {
				Env.dispatchEvent(new  CustomerEvent("statPurchaseOrderByMontSuccess",e.result));
			}
		}
		
		/**
		 * 根据合同号提取可以入库的采购产品列表
		 * @param billNum
		 */
		public function getPurchaseOrderProductList(billNum:String):void{
			_purchaseOrderService.addEventListener(FaultEvent.FAULT, _Fault);
		 	_purchaseOrderService.addEventListener(ResultEvent.RESULT,getPurchaseOrderProductListProcess);
			_purchaseOrderService.getPurchaseOrderProductList(billNum);
		}
		
		private function getPurchaseOrderProductListProcess(e:ResultEvent):void{
			_purchaseOrderService.removeEventListener(ResultEvent.RESULT,getPurchaseOrderProductListProcess);
			Env.dispatchEvent(new CustomerEvent("getPurchaseProductArraySuccess",e.result));
		}
		
		/**
		 * 根据采购合同编号提取采购合同
		 * 
		 */
		public function getPOByPON(pon:String):void{
			_purchaseOrderService.addEventListener(FaultEvent.FAULT, _Fault);
		 	_purchaseOrderService.addEventListener(ResultEvent.RESULT,getPOByPonProcess);
			_purchaseOrderService.getPOByPON(pon);
		}
		
		private function getPOByPonProcess(e:ResultEvent):void{
			_purchaseOrderService.removeEventListener(ResultEvent.RESULT,getPOByPonProcess);
			Env.dispatchEvent(new CustomerEvent("getPOByPonSuccess", e.result));
		}
		
		/**
		 * 查看采购统计明细，按照产品
		 * @param e
		 * 
		*/  
		public  function productDetailSimple(startDate:Date, endDate:Date,name:String):void {		
				_purchaseOrderService.addEventListener(FaultEvent.FAULT, _Fault);
		 		_purchaseOrderService.addEventListener(ResultEvent.RESULT,productDetailSimpleSuccess);
				_purchaseOrderService.getproductDetailSimple(startDate,endDate,name);				
		  }
		/**
		 * 查看采购统计明细，按照产品回调函数
		 * @param e
		 * 
		*/  
		private function productDetailSimpleSuccess(e:ResultEvent):void {
			_purchaseOrderService.removeEventListener(ResultEvent.RESULT, productDetailSimpleSuccess);
			if(e.result != null) {
				purchaseOrderLocator.statDetail.removeAll();
				for each(var result:Array in e.result) {
					var obj:Object = new Object();
					obj.name = result[0];
					obj.color = result[1];
					obj.meterNum = result[2];
					obj.moneyNum = result[3];
					purchaseOrderLocator.statDetail.addItem(obj);
				}
			}
		}
		
		/**
		 * 查看采购统计明细，按照供应商
		 * @param e
		 * 
		*/  
		public  function suppliyDetailSimple(startDate:Date, endDate:Date,pid:String):void {		
				_purchaseOrderService.addEventListener(FaultEvent.FAULT, _Fault);
		 		_purchaseOrderService.addEventListener(ResultEvent.RESULT,suppliyDetailSimpleSuccess);
				_purchaseOrderService.getsuppliyDetailSimple(startDate,endDate,pid);				
		  }
		/**
		 * 查看采购统计明细，按照供应商回调函数
		 * @param e
		 * 
		*/  
		private function suppliyDetailSimpleSuccess(e:ResultEvent):void {
			_purchaseOrderService.removeEventListener(ResultEvent.RESULT, suppliyDetailSimpleSuccess);
			if(e.result != null) {
				purchaseOrderLocator.statDetail.removeAll();
				for each(var result:Array in e.result) {
					var obj:Object = new Object();
					obj.name = result[0];
					obj.color = result[1];
					obj.meterNum = result[2];
					obj.moneyNum = result[3];
					purchaseOrderLocator.statDetail.addItem(obj);
				}
			}
		}
		
		
	}
}